﻿namespace LeadMaster.Controllers
{
  using System.Data.Entity;
  using System.Linq;
  using System.Web.Mvc;
  using LeadMaster.DAL;
  using LeadMaster.Helpers;
  using System.Collections.Generic;
  using LeadMaster.Models;
  using LeadMaster.ViewModels;

  public class ReferralCatsController : Controller
    {

      private DunsmoreContext db = new DunsmoreContext();

        public ActionResult Index()
        {
          Maintenance.inMaintenance = true;
          List<ReferralCats> ListReferralCats = db.ReferralCats.ToList();
          List<ReferralCatsWithReferred> ListReferralCatsWithReferred = new List<ReferralCatsWithReferred>();
          foreach (var item in ListReferralCats)
          {
            ReferralCatsWithReferred rc = new ReferralCatsWithReferred();
            rc.ReferralCat = item;
            rc.RelatedDescription = item.ParentReferralCatsID==0 ? string.Empty : db.ReferralCats.Where(m => m.ReferralCatsID == item.ParentReferralCatsID).FirstOrDefault().Description;
            ListReferralCatsWithReferred.Add(rc);
          }
          return View(ListReferralCatsWithReferred);
        }

        public ActionResult Edit(int referralCatsID)
        {
          ReferralCats rc = db.ReferralCats.Find(referralCatsID);
          ViewBag.Categories = db.ReferralCats
                                        .Where(m => m.ReferralCatsID!=referralCatsID)
                                        .ToList()
                                        .OrderBy(m => m.Description)
                                        .Select(c => new SelectListItem
                                        {
                                          Value = c.ReferralCatsID.ToString(),
                                          Text = c.Description
                                        });
          return View(rc);
        }

        [HttpPost]
        public ActionResult Edit(ReferralCats rc)
        {
          if (ModelState.IsValid)
          {
            db.Entry(rc).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
          }
          ViewBag.Categories = db.ReferralCats
                                        .ToList()
                                        .OrderBy(m => m.Description)
                                        .Select(c => new SelectListItem
                                        {
                                          Value = c.ReferralCatsID.ToString(),
                                          Text = c.Description
                                        });
          return View(rc);
        }


        public ActionResult Create()
        {
          ReferralCats ReferralCat = new ReferralCats();
          ViewBag.ParentReferralCatsID = new SelectList(db.ReferralCats.OrderBy(m => m.Description), "ReferralCatsID", "Description");
          return View(ReferralCat);
        }

        [HttpPost]
        public ActionResult Create(ReferralCats ReferralCat)
        {
          db.ReferralCats.Add(ReferralCat);
          db.SaveChanges();
          return RedirectToAction("Index");
        }

        public ActionResult Delete(int referralCatsID)

        {
          db.ReferralCats.Remove(db.ReferralCats.Find(referralCatsID));
          db.SaveChanges();
          return RedirectToAction("Index");
        }

        public ActionResult MaintainItems(int id)
        {
          ViewModelReferralData ViewModel = new ViewModelReferralData();
          ViewModel.ReferralCat = db.ReferralCats.Find(id);
          ViewModel.ReferralCatsID = id;
          int ParentID = ViewModel.ReferralCat.ParentReferralCatsID;
          int firstReferralDataID=0;
          if (ParentID != 0)
          {
            firstReferralDataID = db.ReferralData.Where(m => m.ReferralCatsID == ParentID).OrderBy(m => m.Description).FirstOrDefault().ReferralDataID;
          }
          else
          {
            firstReferralDataID = 0;
          }
          ViewBag.ParentReferralDataID = new SelectList(db.ReferralData.Where(m => m.ReferralCatsID == ParentID).OrderBy(m => m.Description), "ReferralDataID", "Description",
            firstReferralDataID);
          foreach (var item in db.ReferralData.Where(m => m.ParentReferralDataID == firstReferralDataID && m.ReferralCatsID==id))
          {
            ViewModel.ListReferralData += item.Description + ",";
          }
          if (ViewModel.ListReferralData!=null)
          {
            ViewModel.ListReferralData = ViewModel.ListReferralData.Substring(0, ViewModel.ListReferralData.Length - 1);  
          }
          
          return View(ViewModel);
        }

        [HttpPost]
        public ActionResult MaintainItems(ViewModelReferralData ViewModel)
        {
          // Remove any that are not in our list
          List<string> datalist = ViewModel.ListReferralData.Split(',').ToList();
          List<ReferralData> referralDataIDs = db.ReferralData
            .Where(m => m.ReferralCatsID == ViewModel.ReferralCatsID && m.ParentReferralDataID==ViewModel.ParentReferralDataID).ToList();
          foreach (var item in referralDataIDs)
          {
            if (!datalist.Contains(item.Description))
            {
              db.ReferralData.Remove(item);
            }
          }
          foreach (var item in ViewModel.ListReferralData.Split(','))
          {
            ReferralData rd=db.ReferralData.Where(m => m.Description==item).FirstOrDefault();
            if (rd == null)
            {
              rd = new ReferralData();
              rd.Description = item;
              rd.ParentReferralDataID = ViewModel.ParentReferralDataID;
              rd.ReferralCatsID = ViewModel.ReferralCatsID;
              db.ReferralData.Add(rd);
            }
            else
            {
              rd.ParentReferralDataID = ViewModel.ParentReferralDataID;
              rd.ReferralCatsID = ViewModel.ReferralCatsID;
              db.Entry(rd).State = EntityState.Modified;
            }
          }
          db.SaveChanges();
          return RedirectToAction("Index");
        }

        public JsonResult UpdateParentReferralData(int referralDataID)
        {
          var lds = new SelectList(db.ReferralData.Where(m => m.ParentReferralDataID == referralDataID), "ReferralDataID", "Description");

          return Json(lds, JsonRequestBehavior.AllowGet);
        }
    }
}
